﻿using CorpStore.Common;
using StoreHelper;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

namespace CorpStore.Models
{
    /// <summary>
    /// This class represents a store app and associated attributes therein.  This class MUST match the definition 
    /// contained in CorpStoreServer.PackageInfo.cls since all master catalog data is serialized to JSON using this
    /// definition.  
    /// </summary>
    public class PackageInfo : BindableBase
    {
        public string Name { get; set; }                    // internal name of application; not same as DisplayName
        public string FamilyName { get; set; }              // combination of name + publisherID; is unique
        public string CreateDate { get; set; }              // date that package was installed
        public string InstalledLocation { get; set; }       // full path to where the application was installed
        public string AppxFn { get; set; }                  // filename of source *.appx file used to install this app
        public string StoreID { get; set; }                 // unique store GUID representing this app; null if not an app available from Windows Store
        public string ExeName { get; set; }                 // win32 exe name if this is a win32 app; null otherwise

        public string DisplayName { get; set; }             // localized, UI presentable name of this app
        public string PublisherDisplayName { get; set; }    // UI presentable name of publisher who wrote this app
        public string Version { get; set; }                 // app version number currently installed
        public string Description { get; set; }             // full description of this app
        public string GroupName { get; set; }               // name of group this app belongs to
        public string Identity { get; set; }                // short text (e.g. 'App') obtained from package.Identity; this text is required in order to launch programatically

        private string splashScreenFn;                      // path + filename of splashscreen image
        public string SplashScreenFn
        {
            get { return splashScreenFn; }
            set { splashScreenFn = value; OnPropertyChanged(); OnPropertyChanged("SplashScreenImage"); }
        }
        private BitmapImage splashScreenImage = null;
        public BitmapImage SplashScreenImage
        {
            get
            {
                if (splashScreenImage == null)
                {
                    splashScreenImage = new BitmapImage();
                    if (splashScreenFn.IndexOf("://") == -1)
                        GenIO.SetImageFromLocalFolderAsync(splashScreenImage, getFnWithoutLocalFolder(splashScreenFn));
                    else
                        GenIO.SetImageFromUriAsync(splashScreenImage, splashScreenFn);
                }

                return splashScreenImage;
            }
        }       // actual bitmap image representation of splashscreen


        private string logoFn;                              // path + filename of smaller logo image (typically image which is shown on start screen)
        public string LogoFn
        {
            get { return logoFn; }
            set { logoFn = value; OnPropertyChanged(); OnPropertyChanged("LogoImage"); }
        }
        private BitmapImage logoImage = null;
        public BitmapImage LogoImage
        {
            get
            {
                if (logoImage == null)
                {
                    logoImage = new BitmapImage();
                    if (FirstScreenshotFn.IndexOf("://") == -1)
                        GenIO.SetImageFromLocalFolderAsync(logoImage, getFnWithoutLocalFolder(logoFn));
                    else
                        GenIO.SetImageFromUriAsync(logoImage, logoFn);
                }

                return logoImage;
            }
        }

        private List<ScreenshotInfo> screenshots;               // list of screenshots associated with this app 
        public List<ScreenshotInfo> Screenshots
        {
            get { return screenshots; }
            set { screenshots = value; OnPropertyChanged(); OnPropertyChanged("FirstScreenshotImage"); }
        }

        private ObservableCollection<string> features;      // list of features as derived from Windows Store web service
        public ObservableCollection<string> Features 
        {
            get
            {
                return features;
            }
            set
            {
                features = value; 
                OnPropertyChanged(); 
            }
        }

        public string Args { get; set; }                    // any possible command line arguments which should get passed to this app when launched
            
        public bool IsFeatured { get; set; }                // true if this app should be one of the apps featured in the left side flipView of corpStore

        /// <summary>
        /// instantiate new instances of all lists
        /// </summary>
        public PackageInfo()
        {
            Screenshots = new List<ScreenshotInfo>();
            Features = new ObservableCollection<string>();
        }


        public string ShortDescription                      // shortened version of Description property, limited to 20 char max
        {
            get
            {
                if (Description != null)
                    return Description.Substring(0, Math.Min(Description.Length, 20));
                else
                    return "";
            }
        }

        
        public string FirstScreenshotFn                     // filename of first screenshot, if any
        {
            get
            {
                if (Screenshots.Count > 0)
                    return Screenshots[0].Filename;
                else
                    return SplashScreenFn;
            }
        }


        private BitmapImage firstScreenshotImage;
        public BitmapImage FirstScreenshotImage
        {
            get
            {
                if (firstScreenshotImage == null)
                {
                    firstScreenshotImage = new BitmapImage();
                    if (FirstScreenshotFn.IndexOf("://") == -1)
                        GenIO.SetImageFromLocalFolderAsync(firstScreenshotImage, getFnWithoutLocalFolder(FirstScreenshotFn));
                    else
                        GenIO.SetImageFromUriAsync(firstScreenshotImage, FirstScreenshotFn);
                }

                return firstScreenshotImage;
            }
        }



        public eAppType AppType()                           // returns storeApp (found on Windows Store), win32App (legacy win32 style application), or enterpriseApp (modern app not found on Windows Store)
        {
            eAppType myType = eAppType.unknown;
            if (!string.IsNullOrEmpty(StoreID))
                myType = eAppType.storeApp;
            else if (!string.IsNullOrEmpty(ExeName))
                myType = eAppType.win32App;
            else
                myType = eAppType.enterpriseApp;

            return myType;
        }


        private string getFnWithoutLocalFolder(string fn)
        {
            return string.Format("AppFiles\\{0}", Path.GetFileName(fn));
        }


    }


    public enum eAppType
    {
        storeApp,                   // app found on Windows Store
        enterpriseApp,              // modern win8 app NOT found on Windows Store 
        win32App,                   // legacy win32 application
        unknown,                    // unknown type app
    }


}
